package com.iw.sms.quartz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.iw.adoms.dao.GoodsDao;
import com.iw.adoms.entity.Goods;
import com.iw.adoms.entity.Order;
import com.iw.adoms.entity.neum.Status;

public class AutoComplete extends HibernateDaoSupport {
	@Autowired
	@Resource
	private GoodsDao Dao;

	// 每天调用一次 三天不手动处理确认收货则自动收货
		// System.out.println("进来了、");
	public void autoSaveOrder() {
		List<Order> list = new Vector();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String date = f.format(new Date());
		list=this.getHibernateTemplate()
				.find(" from Order o where  o.status=12 and o.department>2 and o.type = '1'");
		Calendar c_b = Calendar.getInstance();
		Calendar c_e = Calendar.getInstance();
		if (list != null && list.size() > 0) {
			int i = 0;
			for (i = 0; i < list.size(); i++) {
				int days = 0;
				String Sdate = f.format(list.get(i).getReceivedTime());
				try {
					c_b.setTime(f.parse(Sdate));
					c_e.setTime(f.parse(date));
					while (c_b.before(c_e)) {
						days++;
						c_b.add(Calendar.DAY_OF_YEAR, 1);
					}

				} catch (ParseException pe) {
					System.out.println("日期格式必须为：yyyy-MM-dd；如：0010-4-4.");
				}
				if (days > 3 ) {
					Order order = list.get(i);
					super.getSession()
							.createSQLQuery(
									"update adoms.order_order set status=13 where id = "
											+ order.getId()).executeUpdate();
					System.out.println("订单修改成功！");
					// 更新订单中的货物入库
					List<Goods> listG = super.getHibernateTemplate().find(
							"from Goods g where g.order=" + order.getId());
					for (Goods o : listG) {
						Goods g = o.clone(o);
						this.getSession().createSQLQuery("delete from adoms.goods where order_id = "+order.getId()).executeUpdate();
						g.setDepartment(order.getDepartment());
						g.setState(Status.WILL_SALE.getValue());
						g.setOrder(null);
						g.setInsertTime(new Date());
						this.getHibernateTemplate().save(g);
						System.out.println("修改成功");
					}
				}
			}
		}

	}
}
